class Solution {
public:
    int triangleNumber(vector<int>& nums) {
        sort(nums.begin(),nums.end());
        int count=0;
        int max_idx=nums.size()-1;
        
        while(max_idx>=2)
        {
            int left=0; int right=max_idx-1;
           while(left<right)
           {
             if(nums[left]+nums[right]>nums[max_idx])
            {
                count+=(right-left);
                 right--;
                
            }
            else
            {
                left++;
            }
           }
            max_idx--;
        }
        return count;
    }
};
